package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.service.IBankAccountService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 服务端银行账户服务实现类
 * @author 45505
 */
@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {
    @Override
    public void addOrUpdate(BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {
        if (Objects.isNull(bankAccountUpsertReqDTO)){
            throw new BadRequestException("账户信息不能为空");
        }
        if (!(bankAccountUpsertReqDTO.getType() == CertificationStatusEnum.SUCCESS.getStatus()
                || bankAccountUpsertReqDTO.getType() == CertificationStatusEnum.PROGRESSING.getStatus())){
            throw new BadRequestException("非服务人员不能操作");
        }
        if (Objects.isNull(bankAccountUpsertReqDTO.getId())){
            BankAccount bankAccount = BeanUtils.toBean(bankAccountUpsertReqDTO, BankAccount.class);
            if (bankAccountUpsertReqDTO.getType()==1){
                bankAccount.setType(3);
            }
            bankAccount.setCreateTime(LocalDateTime.now());
            bankAccount.setUpdateTime(LocalDateTime.now());
            boolean save = save(bankAccount);
            if (!save){
                throw new BadRequestException("新增失败");
            }
            return;
        }
        BankAccount bankAccount = BeanUtils.toBean(bankAccountUpsertReqDTO, BankAccount.class);
        bankAccount.setUpdateTime(LocalDateTime.now());
        boolean update = updateById(bankAccount);
        if (!update){
            throw new BadRequestException("更新失败");
        }
    }
}
